package com.sakura.probe.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sakura.probe.common.api.CommonResult;
import com.sakura.probe.common.util.PageCalculate;
import com.sakura.probe.mapper.CommentMapper;
import com.sakura.probe.mapper.PersonInfoMapper;
import com.sakura.probe.pojo.Comment;
import com.sakura.probe.pojo.PersonInfo;
import com.sakura.probe.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {
    @Autowired
    private CommentMapper commentMapper;

    @Override
    public List<Comment> listByCondition(Comment condition, int pageIndex, int pageSize) {
       List<Comment> list = commentMapper.queryComment(condition,
                PageCalculate.calculateRowIndex(pageIndex, pageSize), pageSize);
        //计算每条评论的回复数
        int n = list.size();
        Comment comment;
        Comment temp;
        for (int i=0;i<n;i++){
            comment = list.get(i);
            temp = new Comment();
            temp.setParentId(comment.getTopicCommentId());
            comment.setCommentNum(commentMapper.queryCommentCount(temp));
            list.set(i,comment);
        }
        return list;
    }

    @Override
    public Comment listByCondition(Comment condition, Long commentId, int pageIndex, int pageSize) {
        //获取主评论
        //构造条件获取主评论人的信息
        Comment comment = new Comment();
        comment.setTopicCommentId(commentId);
        Comment result = commentMapper.queryComment(comment,0,1).get(0);
        //获取主评论下面子评论的列表
        List<Comment> list = commentMapper.queryComment(condition, PageCalculate.calculateRowIndex(pageIndex, pageSize), pageSize);
        result.setCommentList(list);
        return result;
    }

    @Override
    public Long countByCondition(Comment conditon) {
        return commentMapper.queryCommentCount(conditon);
    }
}
